/*  
 * @(#) DemoXmlUserAction.java Create on 2012-5-11 上午10:03:21   
 *   
 * Copyright 2012 by xl.   
 */

package cn.com.demo.xml.action;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.com.common.Constants;
import cn.com.common.base.action.BaseAction;
import cn.com.common.base.bean.BackstageResult;
import cn.com.common.base.bean.PageBean;
import cn.com.demo.bean.DemoUserBean;
import cn.com.demo.xml.service.DemoXmlUserService;
import cn.tools.DateHelper;

/**
 * 
 * @author zhanghongliang
 * @date 2012-5-11
 */
@Controller
@Scope("prototype")
public class DemoXmlUserAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6688922226403175650L;

	@Resource
	DemoXmlUserService service;
	private DemoUserBean oprBean = new DemoUserBean();
	private DemoUserBean queryBean = new DemoUserBean();

	public String toIndex() throws Exception {
		DemoUserBean query = this.getQueryBean();
		PageBean<DemoUserBean> page = service.getUserPage(query);
		putToContext("pageItem", page);
		return TOINDEX;
	}

	public String save() throws Exception {
		BackstageResult msg = service.save(this.getOprBean());
		putToContext(Constants.SYSTEM_ACTION_RESUT_MSG, msg);
		return TOEDIT;
	}

	public String delete() throws Exception {
		BackstageResult msg = service.deltet(getOprBean());
		putToContext(Constants.SYSTEM_ACTION_RESUT_MSG, msg);
		return TOINDEX;
	}

	public String edit() throws Exception {
		DemoUserBean demo = service.find(oprBean.getId());
		setOprBean(demo);
		return TOEDIT;
	}

	@Override
	public void validate() {
		super.validate();
	}

	public void validateSave() {
		System.out.println("save validate");
	}

	/**
	 * 查询时校验 不正确的数据格式，清空查询内容
	 * 
	 * @Title: validateToIndex
	 * @data:2012-5-10下午4:09:08
	 * @author:zhanghongliang
	 * 
	 */
	public void validateToIndex() {
		String b_gt = queryBean.get_birthday_condtion_GT();
		String b_lt = queryBean.get_birthday_condtion_LT();
		if (StringUtils.isNotBlank(b_gt) && !DateHelper.DATE_PATTERN.matcher(b_gt).matches()) {
			queryBean.set_birthday_condtion_GT(null);
		}
		if (StringUtils.isNotBlank(b_lt) && !DateHelper.DATE_PATTERN.matcher(b_lt).matches()) {
			queryBean.set_birthday_condtion_LT(null);
		}
	}

	public DemoUserBean getOprBean() {
		return oprBean;
	}

	public void setOprBean(DemoUserBean oprBean) {
		this.oprBean = oprBean;
	}

	public DemoUserBean getQueryBean() {
		return queryBean;
	}

	public void setQueryBean(DemoUserBean queryBean) {
		this.queryBean = queryBean;
	}
}
